package com.code.other;

/**
 * https://blog.csdn.net/C3245073527/article/details/121938276
 */
public class HuiXing {
    public static void main(String[] args) {
//        int[][] arr = getArr(4,3);
//        int[][] arr = getArr(3,4);
        int[][] arr = getArr(6);
        for (int[] i : arr) {
            for (int j : i) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }

    public static int[][] getArr(int n) {
        int index =1;
        int top = 0;
        int bottom = n -1;
        int left = 0;
        int right = n-1;
        int[][] arr = new int[n][n];
        while (left < right ) {
            for (int i = left;  i<= right; i++) {
                arr[top][i] = index++;
            }
            top++;
            for (int i = top;  i<= bottom; i++) {
                arr[i][right] = index++;
            }
            right--;

            for (int i = right; i>= left;i--) {
                arr[bottom][i] = index++;
            }
            bottom--;

            for (int i = bottom; i>= top;i--) {
                arr[i][left] = index++;
            }
            left++;

        }
        System.out.println(String.format("top:%s, bottom:%s, left:%s, right:%s",top, bottom, left, right));
        return arr;

    }


    public static int[][] getArr(int x, int y) {
        int index =1;
        int top = 0;
        int bottom = x-1;
        int left = 0;
        int right = y-1;
        int[][] arr = new int[x][y];
        while (left <= right ) {
            for (int i = left;  i<= right; i++) {
                arr[top][i] = index++;
            }
            top++;
            for (int i = top;  i<= bottom; i++) {
                arr[i][right] = index++;
            }
            right--;

            for (int i = right; i>= left;i--) {
                arr[bottom][i] = index++;
            }
            bottom--;

            for (int i = bottom; i>= top;i--) {
                arr[i][left] = index++;
            }
            left++;

        }
        System.out.println(String.format("top:%s, bottom:%s, left:%s, right:%s",top, bottom, left, right));
        return arr;

    }



}
